package com.example.Windturbine.Controller;

import com.alibaba.excel.EasyExcel;
import com.example.Windturbine.Controller.ViewObject.Excel.MaterialExcel;
import com.example.Windturbine.Controller.ViewObject.MaterialInfoVO;
import com.example.Windturbine.Controller.ViewObject.MaterialVO;
import com.example.Windturbine.DataObject.MaterialDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Listener.ExcelMaterialListener;
import com.example.Windturbine.Response.CommonResult;
import com.example.Windturbine.Service.MaterialService;
import com.example.Windturbine.Service.Model.MaterialModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import static com.example.Windturbine.Controller.BaseController.CONTENT_TYPE_FORMED;

/**
 * @Author 丁永新
 * @Date 2021/12/15
 */
@Slf4j
@Controller("material")
@RequestMapping("/material")
@CrossOrigin(allowCredentials="true", allowedHeaders = "*")
public class MaterialController {
    @Autowired
    MaterialService materialService;

    @RequestMapping("/get")
    @ResponseBody
    public CommonResult getAllMaterial(@RequestParam(name="id")Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException{
        List<MaterialVO> res=convertFromMaterialDO(materialService.getAll(id));
        return CommonResult.create(res);
    }

    @RequestMapping(value = "/submit",method = {RequestMethod.POST},consumes={CONTENT_TYPE_FORMED})
    @ResponseBody
    public CommonResult submit(@RequestParam(name="mat_name")String mat_name,
                               @RequestParam(name="mat_type")String mat_type,
                               @RequestParam(name="mat_unit")String mat_unit,
                               @RequestParam(name="mat_region")String mat_region,
                               @RequestParam(name="mat_origin")String mat_origin,
                               @RequestParam(name="mat_comments")String mat_comments,
                               @RequestParam(name="coal") BigDecimal coal,
                               @RequestParam(name="oil")BigDecimal oil,
                               @RequestParam(name="gas")BigDecimal gas,
                               @RequestParam(name="co")BigDecimal co,
                               @RequestParam(name="co2")BigDecimal co2,
                               @RequestParam(name="so2")BigDecimal so2,
                               @RequestParam(name="nox")BigDecimal nox,
                               @RequestParam(name="ch4")BigDecimal ch4,
                               @RequestParam(name="h2s")BigDecimal h2s,
                               @RequestParam(name="cod")BigDecimal cod,
                               @RequestParam(name="hcl")BigDecimal hcl,
                               @RequestParam(name="nh3")BigDecimal nh3,
                               @RequestParam(name="cfc11")BigDecimal cfc11,
                               @RequestParam(name="cfc12")BigDecimal cfc12,
                               @RequestParam(name="cfc113")BigDecimal cfc113,
                               @RequestParam(name="id")Integer level) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {

        MaterialModel materialModel=new MaterialModel();
        materialModel.setName(mat_name);
        materialModel.setType(mat_type);
        materialModel.setUnit(mat_unit);
        materialModel.setRegion(mat_region);
        materialModel.setOrigin(mat_origin);
        materialModel.setComments(mat_comments);
        materialModel.set煤(coal);
        materialModel.set天然气(gas);
        materialModel.set石油(oil);
        materialModel.setCo(co);
        materialModel.setCo2(co2);
        materialModel.setSo2(so2);
        materialModel.setNox(nox);
        materialModel.setCh4(ch4);
        materialModel.setH2s(h2s);
        materialModel.setCod(cod);
        materialModel.setHcl(hcl);
        materialModel.setNh3(nh3);
        materialModel.setCfc11(cfc11);
        materialModel.setCfc12(cfc12);
        materialModel.setCfc113(cfc113);
        materialModel.setLevel(level);

        materialService.submitSingle(materialModel);

        return CommonResult.create(null);
    }


    @RequestMapping(value = "/submitfile", method = {RequestMethod.POST}, consumes = {"application/x-www-form-urlencoded", "multipart/form-data"})
    @ResponseBody
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public CommonResult submitProduct(@RequestParam(name = "userVOIdFile") Integer userid,@RequestParam(name = "uploadfile") MultipartFile file) throws BusinessException, IOException, NoSuchAlgorithmException {
        if (file == null) {
            log.error("submitProduct：上传文件失败！");
            throw new BusinessException(new BusinessException(EmBusinessError.UNKNOWN_ERROR, "上传文件失败！"));
        }
        System.out.println(file.getOriginalFilename());
        InputStream in = file.getInputStream();
        EasyExcel.read(in, MaterialExcel.class, new ExcelMaterialListener(materialService, userid)).sheet().doRead();
        in.close();
        return CommonResult.create(null);
    }









    @RequestMapping("/getdetail")
    @ResponseBody
    public CommonResult getDeatailById(@RequestParam(name="id")Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException{
        MaterialModel materialInfoDO= materialService.getDetailById(id);
        MaterialInfoVO materialInfoVO=convertFromSingleMaterialDO(materialInfoDO);
        return CommonResult.create(materialInfoVO);
    }

    @RequestMapping("/deletebyid")
    @ResponseBody
    public CommonResult deleteById(@RequestParam(name="id")Integer id) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException{
        materialService.deleteById(id);
        return CommonResult.create(null);
    }

    private List<MaterialVO> convertFromMaterialDO(List<MaterialDO> list){
        List<MaterialVO> res=new ArrayList<>();
        if(list.size()==0) return res;
        for(MaterialDO i:list){
            res.add(new MaterialVO(i.getId(),i.getName(),i.getType(),i.getUnit(),i.getRegion(),i.getOrigin(),i.getComments()));
        }
        return res;
    }

    private MaterialInfoVO convertFromSingleMaterialDO(MaterialModel materialModel){
        MaterialInfoVO materialInfoVO=new MaterialInfoVO(materialModel.getId(),materialModel.get煤(),materialModel.get石油(),materialModel.get天然气(),
                materialModel.getCo(),materialModel.getCo2(),materialModel.getSo2(),materialModel.getNox(),materialModel.getCh4(),materialModel.getH2s(),
                materialModel.getHcl(),materialModel.getCod(),materialModel.getNh3(),materialModel.getCfc11(),materialModel.getCfc12(),materialModel.getCfc113());

        return  materialInfoVO;

    }


}
